import React, { createContext, useContext } from "react";

const NavContext = createContext(null);
const history = window.history;

export default function FuncComRouter() {
  return (
    <NavContext.Provider value={history}>
      <ChildRouter />
    </NavContext.Provider>
  );
}

const withRouter = (Component) => {
  return function WithRouterComponent() {
    const history = useContext(NavContext);
    return <Component history={history} />;
  };
};

const Child = ({ history }) => {
  return (
    <div>
      <button onClick={() => history.pushState({}, undefined, "/hello")}>
        to hello
      </button>
    </div>
  );
};

const ChildRouter = withRouter(Child);
